﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Phukien : System.Web.UI.Page
{
    PagedDataSource pds = new PagedDataSource();

    PhukienController phukienControl;

    DataTable dtPhukien;
    protected void Page_Load(object sender, EventArgs e)
    {
        phukienControl = new PhukienController();
        dtPhukien = phukienControl.GetAll();

        bindGrid();
    }

    public int CurrentPage
    {
        get
        {
            if (this.ViewState["CurrentPage"] == null)
                return 0;
            else
                return Convert.ToInt16(this.ViewState["CurrentPage"].ToString());
        }
        set
        {
            this.ViewState["CurrentPage"] = value;
        }
    }

    public void doPaging()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("PageIndex");
        dt.Columns.Add("PageText");
        for (int i = 0; i < pds.PageCount; i++)
        {
            DataRow dr = dt.NewRow();
            dr[0] = i;
            dr[1] = i + 1;

            dt.Rows.Add(dr);
        }

        dlPaging.DataSource = dt;
        dlPaging.DataBind();

    }

    public void bindGrid()
    {
        pds.DataSource = dtPhukien.DefaultView;
        pds.AllowPaging = true;
        pds.PageSize = 12;
        pds.CurrentPageIndex = CurrentPage;
        //lnkbtnNext.Enabled = !pds.IsLastPage;
        //lnkbtnPrevious.Enabled = !pds.IsFirstPage;

        dtlPhukien.DataSource = pds;
        dtlPhukien.DataBind();

        doPaging();
    }

    protected void dlPaging_ItemCommand(object sender, DataListCommandEventArgs e)
    {
        if (e.CommandName.Equals("lnkbtnPaging"))
        {
            CurrentPage = Convert.ToInt16(e.CommandArgument.ToString());
            bindGrid();
        }
    }

    //protected void lnkbtnPrevious_Click(object sender, EventArgs e)
    //{
    //    CurrentPage -= 1;
    //    bindGrid();
    //}

    //protected void lnkbtnNext_Click(object sender, EventArgs e)
    //{
    //    CurrentPage += 1;
    //    bindGrid();
    //}

    protected void dlPaging_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        LinkButton lnkbtnPage = (LinkButton)e.Item.FindControl("lnkbtnPaging");
        if (lnkbtnPage.CommandArgument.ToString() == CurrentPage.ToString())
        {
            lnkbtnPage.Enabled = false;
            lnkbtnPage.Font.Bold = true;
        }
    }
}